package com.youlu.campus.service.integral;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.UserIntegralDetail;
import com.youlu.campus.entity.UserIntegralPO;
import com.youlu.campus.service.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 * Created by zhuhuaiqi on 2020/11/18.
 */
@Service
@Slf4j
public class UserIntegralService extends BaseService {

    /**
     * 分享次数
     */
    public static final int SHARE_TIME = 3;
    /**
     * 点赞次数
     */
    public static final int LIKE_TIME = 3;
    /**
     * 评论次数
     */
    public static final int COMMENT_TIME = 3;


    public boolean addIntegral(UserIntegralPO userIntegralPO) {
        log.info(":>>> 开始累加积分:{}", JSON.toJSONString(userIntegralPO));
        Query query = new Query(Criteria.where("userId").is(userIntegralPO.getUserId()));
        UserIntegralPO integralPO = mongoTemplate.findOne(query, UserIntegralPO.class);
        if (Objects.isNull(integralPO)) {
            log.info(":>>> 用户积分信息为空:{}", userIntegralPO.getUserId());
            userIntegralPO.setCreatedTime(new Date());
            mongoTemplate.insert(userIntegralPO);
        }
        Update update = new Update();
        update.inc("integral", userIntegralPO.getIntegral());
        update.set("updatedTime", new Date());
        mongoTemplate.updateFirst(query, update, UserIntegralPO.class);
        UserIntegralDetail userIntegralDetail = new UserIntegralDetail();
        userIntegralDetail.setUserId(userIntegralPO.getUserId());
        userIntegralDetail.setIntegral(userIntegralPO.getIntegral());
        userIntegralDetail.setConsumeType("add");
        userIntegralDetail.setCreatedTime(new Date());
        userIntegralDetail.setDesc(userIntegralPO.getDesc());
        userIntegralDetail.setBizType(userIntegralPO.getBizType());
        userIntegralDetail.setBizId(userIntegralPO.getBizId());
        userIntegralDetail.setBizOId(userIntegralPO.getBizOId());
        mongoTemplate.insert(userIntegralDetail);
        return true;
    }

    public Integer getIntegral(String userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        UserIntegralPO integralPO = mongoTemplate.findOne(query, UserIntegralPO.class);
        if (integralPO == null) {
            return 0;
        }
        return integralPO.getIntegral();
    }


    private Map<String, Integer> getActivity(String key, Integer maxTimes) {
        Map<String, Integer> activity = new HashMap<>(2);
        activity.put("maxTimes", maxTimes);
        activity.put("finishTimes", getActivityTimes(key));
        return activity;
    }

    private Integer getActivityTimes(String key) {
        String times = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(times)) {
            return Integer.valueOf(times);
        }
        return 0;
    }
}
